package com.share.finance.risk.rules.web.exe;

import com.share.common.common.Result;
import com.share.common.utils.IpUtil;
import com.share.finance.risk.common.model.sys.RuleNode;
import com.share.finance.risk.common.model.sys.RuleRequest;
import com.share.finance.risk.rules.model.core.FactDrls;
import com.share.finance.risk.rules.service.core.ContainerService;
import com.share.finance.risk.rules.service.exe.ExecuteRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@ConditionalOnBean(ContainerService.class)
public class ExecuteRuleController {
    @Autowired
    private ExecuteRuleService executeRuleService;

    //标准执行规则
    @PostMapping("/execute")
    public Result<RuleNode> execute(@RequestBody RuleRequest req, HttpServletRequest request) {
        try {
            RuleNode obj = executeRuleService.exeRule(req, IpUtil.getIpAddress(request));
            return Result.success(obj);
        } catch (Exception e) {
            log.error("接口调用异常", e);
            return Result.failure(e.getMessage());
        }
    }


    //发布人员测试规则
    @PostMapping("/test")
    public Result<RuleNode> test(@RequestBody FactDrls req) {
        try {
            RuleNode obj = executeRuleService.test(req);
            return Result.success(obj);
        } catch (Exception e) {
            log.error("接口调用异常", e);
            return Result.failure(e.getMessage());
        }
    }
}
